package json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.DataParser;
import net.sf.rose.jdbc.bean.FieldBean;
import net.sf.rose.util.DataUtil;

public class JsonHttpRequestBean {

	public static String TABLE_NAME = "tableName";
	public static String CLASS_NAME = "className";

	private String tableName = null;

	private String className = null;

	private Map<String, String> paramemters = null;

	private JSONArray jsonArray = null;

	private List<Object> data = null;

	private int[] actions = null;

	public JsonHttpRequestBean() {
		super();
	}

	public JsonHttpRequestBean(Object data) {
		super();
		this.setJSONObject(data);
	}

	public int size() {
		return data.size();
	}

	public int getAction(int index) {
		return actions[index];
	}

	public Object getBean(int index) {
		return data.get(index);
	}

	public void setJSONObject(Object data) {
		// 提交的数据必须是json数组
		jsonArray = JSONArray.fromObject(data);
		if (jsonArray.size() == 0) {
			return;
		}
		// 数组的第一个元素必须是类型
		paramemters = new HashMap<String, String>();
		JSONObject json = JSONObject.fromObject(jsonArray.getJSONObject(0));
		for (Object obj : json.keySet()) {
			String key = obj.toString();
			paramemters.put(key, json.getString(key));
		}
		this.tableName = paramemters.get(TABLE_NAME);
		this.className = paramemters.get(CLASS_NAME);
		jsonArray.remove(0);
	}

	/**
	 * 利用json-lib.jar将Json格式的数据转换成Java对象格式。
	 */
	public List<Object> parseJson(Class<?> entityClass) {
		data = new ArrayList<Object>();
		actions = new int[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject json = jsonArray.getJSONObject(i);
			data.add(JSONObject.toBean(json, entityClass));
			actions[i] = this.parseActionType(json);
		}
		return data;
	}

	/**
	 * 利用json-lib.jar将Json格式的数据转换成Java对象格式。
	 */
	public List<Object> parseJsonList(String json, Class<?> entityClass) {
		jsonArray = JSONArray.fromObject(json);
		return this.parseJson(entityClass);
	}

	/**
	 * 利用json-lib.jar将Json格式的数据转换成Java对象格式。
	 */
	public Object parseJsonBean(String json, Class<?> entityClass) {
		JSONObject js = JSONObject.fromObject(json);
		actions = new int[1];
		actions[0] = this.parseActionType(js);
		return JSONObject.toBean(js, entityClass);
	}

	private int parseActionType(JSONObject json) {
		Object obj = json.get("_state");
		if (obj == null) {
			return 0;
		}
		if (obj.equals("added")) {
			return DBUtils.TYPE_INSERT_INT;
		} else if (obj.equals("modified")) {
			return DBUtils.TYPE_UPDATE_INT;
		} else if (obj.equals("removed")) {
			return DBUtils.TYPE_DELETE_INT;
		}
		return 0;
	}

	public List<Object> parseJson() {
		Class<?> entityClass = null;
		// 如果定义了数据要转换成的JavaBean对象类型
		if (this.className != null) {
			entityClass = DataUtil.getClass(this.className);
			this.parseJson(entityClass);
		}
		// 如果指定了数据所属的数据库表名
		else if (this.tableName != null) {
			String key = null;
			Object val = null;
			Map<String, FieldBean> fields = DBUtils.getFields(this.tableName);
			DataParser parse = new DataParser();
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				Map<String, Object> map = new HashMap<String, Object>();
				for (FieldBean field : fields.values()) {
					key = field.getField();
					val = json.get(key);
					if (val != null) {
						val = parse.parse(field.getJavaType(), val, null);
						map.put(key, val);
					}
				}
				data.add(map);
				actions[i] = this.parseActionType(json);;
			}
		}
		return data;
	}

	public String getTableName() {
		return tableName;
	}

	public String getClassName() {
		return className;
	}

	public String getParamemter(String key) {
		String str = paramemters.get(key);
		return str == null ? null : (str.equals("null") ? null : str);
	}

	public JSONArray getJsonDataArray() {
		return jsonArray;
	}

	public List<Object> getData() {
		return data;
	}

}
